/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package insulinpumpsimulation.com.his.tasks;

import insulinpumpsimulation.SimulationController;
import insulinpumpsimulation.com.his.audiocontrol.SoundAlert;
import insulinpumpsimulation.com.his.literal.InsulinPumpLiterals;

/**
 *
 * @author Arun
 */
public class BolusManager {

    private static double bolusRateCounter = 0;

    private static SoundAlert speak=null;
    private static String path;
    
    public static double menuItems(String item, String dose) {



        if (item.equalsIgnoreCase("1 portion Döner")) {
            if (validateDailyBolus(2.0)) {
                SimulationController.getInstance()
                        .setIns_Msg_Area_Success("2 units of insulin has been injected into your body as per your request.");
                SimulationController.getInstance()
                    .setIns_txtBox_Manual("2.0");
                return 2.0 / (InsulinPumpLiterals.RESERVOIR_LEVEL_FULL);
            }

        }
        if (item.equalsIgnoreCase("1 portion Tacos")) {
            if (validateDailyBolus(1.5)) {
                SimulationController.getInstance()
                        .setIns_Msg_Area_Success("1.5 units of insulin has been injected into your body as per your request.");
                SimulationController.getInstance()
                    .setIns_txtBox_Manual("1.5");
                return 1.5 / (InsulinPumpLiterals.RESERVOIR_LEVEL_FULL);

            }
        }
            if (item.equalsIgnoreCase("1 Medium Banana")) {
                if (validateDailyBolus(2.0)) {
                    SimulationController.getInstance()
                            .setIns_Msg_Area_Success("2 units of insulin has been injected into your body as per your request.");
                   SimulationController.getInstance()
                    .setIns_txtBox_Manual("2.0");
                    return 2.0 / (InsulinPumpLiterals.RESERVOIR_LEVEL_FULL);
                }

            }
            if (item.equalsIgnoreCase("Manual Entry")) {

                if (dose.matches("[0-9]+(?:[.][0-9]+)?")) {
                    double loc = Double.parseDouble(dose);
                    if (validateDailyBolus(loc)) {
                        if (loc > 0.0 && loc <= 5.0) {
                            SimulationController.getInstance()
                                    .setIns_Msg_Area_Success(loc + " units of insulin has been injected into your body as per your request.");
                            return loc / (InsulinPumpLiterals.RESERVOIR_LEVEL_FULL);
                        } else {
                            SimulationController.getInstance()
                                    .setIns_Msg_Area(" The range for single insulin shot is 0.1 unit to 5 units. Please enter value between between the range");
                            return 0;
                        }
                    }
                } else {
                    SimulationController.getInstance()
                            .setIns_Msg_Area("Please enter only Numeric value.");
                }

            } else {
                return 0;
            }
            return 0;
        }
    
    
    

    public static boolean validateDailyBolus(double bolusAmount) {
        if (DurationCalculator.getCurrentTimeinSeconds() - DurationCalculator.getStartTimeinSeconds() < 720) {

            if (bolusAmount < 5.0)
            {
            bolusRateCounter = bolusRateCounter + bolusAmount;
            }

            if (bolusRateCounter >= 20) {
                SimulationController.getInstance()
                        .setIns_Msg_Area("You have exceeded the daily bolus limit");
                
                path="resources/resources/ExceededDailyBolusLimit.wav";     
                speak=new SoundAlert(path);
                speak.start();
                speak.stop();
                
                return false;
            }
        } else {
            bolusRateCounter = 0;
        }
        return true;

    }
}
